/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov.world;

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class BasicBody implements Body {

	protected Map<String, Property> properties;
	protected Point2D location, speed;
	protected double bearing;
	protected Shape figure;
	
	private int type;
	private double reward;
	
	public BasicBody() {
		properties = new HashMap<String, Property>();
		location = new Point2D.Double();
		speed = new Point2D.Double();
	}
	
	public BasicBody(Shape figure) {
		this();
		setFigure(figure);
	}
	
	@Override
	public double getBearing() {
		return bearing;
	}

	@Override
	public Shape getFigure() {
		return figure;
	}

	public void setFigure( Shape s ) {
		figure = s;
	}
	
	@Override
	public Point2D getLocation() {
		return location;
	}

	@Override
	public Point2D getSpeed() {
		return speed;
	}

	@Override
	public int getType() {
		return type;
	}

	@Override
	public double getX() {
		return location.getX();
	}

	@Override
	public double getY() {
		return location.getY();
	}

	@Override
	public void setLocation(double x, double y) {
		//rubbish
		AffineTransform xForm = AffineTransform.getTranslateInstance(
				x - location.getX(), y - location.getY());
		figure = xForm.createTransformedShape(figure);
		location.setLocation(x, y);
	}

	@Override
	public void setType(int type) {
		this.type = type;
	}

	@Override
	public void addProperty( Property p ) {
		properties.put(p.getName(), p);
	}
	
	@Override
	public Collection<Property> getProperties() {
		return Collections.unmodifiableCollection(properties.values());
	}
	
	@Override
	public Property getProperty( String name ) {
		return properties.get(name);
	}
	
	@Override
	public double getPropertyValue( String name ) {
		return properties.get(name).getValue();
	}
	
	@Override
	public void updateProperty(String name, double delta) {
		Property p = properties.get(name);
		p.update(delta);
	}

	@Override
	public double getReward() {
		return reward;
	}

	@Override
	public void setReward(double r) {
		reward = r;
	}

	@Override
	public void setBearing(double angle) {
		bearing = angle;
		
	}
}
